package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000143666587";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDdZWpLgq3BnrgPMRrHa0NrwEfXEriZ59p5KNQRoKfXnQxFlpfL1L/IezvSDmLiUxtMuBRBYKZ0BkthRun5R5Yj/p8OjH/6xqxdqg+6BgbgQcGQLVzWmKfu5QzLekjnCoRc3iT5xsN1O66MtJJqZEqpfXHVZL4/xhKHwhMgiTOqRfQ7IADZy0j0BauOQgFSvlekDL9D4Q7QuZt7w5edpynqwyW62bO6XeReTbAWg8vRqtwXC/qkgstGJ0BqYCLe2Ih64WRMCHGLaDbvO3kMNuEleFCg9fASkzMhtBBr7T0DniO7hXYvodNbM1xkT0gsn1b27QiJaJRcx4Gfw6rglR3ZAgMBAAECggEBAJTc9gXjzjVcJWKBoAHqi6DN3wAsJFfH38yLuVxI69I8VN4Nje+OE44X3nc1GSQYwUhqlaQH+qQU/2ZfKj9k9P0q3YQI0JysLYWlLYHlP4ZWcHIjLkNUzcYB2QKQLKi9F5bh/3a7QP14rbsKR1CmMtWX9YyIequi4R7LTdUYq9aWho0inuGLnCyRfsYI20fp7ovr8TlhA43m+AxxJ9cci4ZcpFWW3UgDXb24/CbyBnSEqx2rdEgStegDlS/EgebeEkHHIK2MvYWIIRXNZlHEosDku9mCSHT9trJVY0elOl/JCK1vDalKQP42GklyKIRDJ8LzhWXC4nVDg0Wg3A2t4CECgYEA88rSLg1WwoCAM1sK1h9ddT7K1UjLUkib5BXWCROEL06qBc/1JdN2HavBy71brKXPsfiy9yOzCUVJt6h2z/53qe5adIut6FAC0HIBH1ycpexCgGwWfkxFMF4A6/6CarjurKBP675CJc2t3Qdxeoriye/JbhD+nUtP80zeRqyF/OUCgYEA6Ht/cqOudXadKhO692Ayf7d+U2dBfOmnbJ9CqfJ1+CQqoV/XjyzKHUx1US7oxGaqJXgQ2UhfZCctDBcOh4x6bKk3wzBXICAj3bAvjj5EDPiimNo+jqbi2eZ/fJvvwOphJqR3FqWFH8afRuusEw1vMR8Z+xv13hA8TtHYAhz5AeUCgYAMlaCp3ehnm+iCPiXeDbpuTfJ8dgiA09ywNmxZBZfiNUZTjHuUG0lSmMYlNT4hOGHzRuo+FaqtRNVcVKblb7ZPoE0tDPTKNC8hAXGLY+V2Ly7zoCC9HkTIDWIwzG+BANDZOmFFwwl5GBfse/DgqYCxed5E/z2VcwjKf2JN9aPo/QKBgAleWK90+yXOpty4ONOZM8/DxDp2McRn4hBVhqk0lR5vzqv8IcKqSmbN2vhvjH/Fp1ulMAChkv6FOaKAF1zNA233j8C05P3tbaSUYgY4aiOfrWW9fcHf5cpkKIUL/6hliY62F/p4pxm1tlt0SdXmt7FiEHj3EnxlaRjAChVjt4ElAoGBAJb/Gj7nK7+inMSzyGdZ3kPVtDNJ8TW1agRmyVYMpqiTs6MQT1n1V7muVDrT30dw1YiRDoc8rK323bAqd6Rhit3/gva9KvPrvlSdgPQ3wtR9qGXXE9QncGs6ENZvv3M5eN4yULAn8VHq/Go0SBMbYB5C7+9ibLClbEXmphhYINcI";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyFxLxj0qFxzjM1OKopmky1w4vGiv5U7qas3lxJXXYmadFEdqlY8hRdO6MR0nPfsZEV5NrbBdt8P+2XLNIshXgb/X/5WPzVPtJKv7nj850HGBybFMIRoPcUpVSG53l2NlIkrUesMQq6yPD9FiWnF9TJC5+2nXv5THLLiBraEA/UMjnQhg6sPCB4Wd6KXL+3VmDek+0Jb78YTdhlXtdVN7+lyR2sLh55/fk9IDUfZvfrYaeNtDHdG6y67GV8raChPNS8wSLjFSXblug3+rZ8r7oRmnm3WUQKww99ufgz09yhHOkWseiOZvjNAV6VNDDiSTgpkZmdeRq6x5erkdv4nA+wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url=" http://yahx82.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    //超时关单
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
